home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 1
/
Atari Mega Archive - Volume 1.iso
/
language
/
sozobon2.zoo
/
hcc
/
out_st.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-12-14
|
13KB
|
799 lines
/* Copyright (c) 1988 by Sozobon, Limited. Author: Johann Ruegg
*
* Permission is granted to anyone to use this software for any purpose
* on any computer system, and to redistribute it freely, with the
* following restrictions:
* 1) No charge may be made other than reasonable charges for reproduction.
* 2) Modified versions must be clearly marked as such.
* 3) The authors are not responsible for any harmful consequences
* of using this software, even if they result from defects in it.
*
* out.c
*
* Code generation output routines.
*/
#include <stdio.h>
#include "param.h"
#include "nodes.h"
#include "flags.h"
#include "bstok.h"
#include "tytok.h"
#include "gen.h"
#if dLibs
#include <ctype.h>
#endif
#if MMCC
overlay "pass2"
#endif
#if CC68
FILE *fopen();
#endif
#if NEEDBUF
char my_obuf[BUFSIZ];
#endif
#define T_SEG 0
#define D_SEG 1
#define B_SEG 2
#define TO_TEXT to_seg(T_SEG)
#define TO_DATA to_seg(D_SEG)
#define TO_BSS to_seg(B_SEG)
#define isareg(np) ((np)->g_token == REGVAR && (np)->g_rno >= AREG)
extern FILE *output;
static int in_seg;
static int lblnum;
static int dat_size;
out_start(outs)
char *outs;
{
register int len;
char suff;
#ifdef MINIX
extern char tmpdir[];
#endif
char oname[128];
len = strlen(outs);
if (len >= 2 && outs[len-2] == '.') {
suff = outs[len-1];
if (suff != 'c' && suff != 'C')
fatals("Invalid suffix", outs);
#ifdef MINIX
sprintf(oname, "%s/%s", tmpdir, outs);
len = strlen(oname);
#else
strcpy(oname, outs);
#endif
oname[len-1] = 's';
output = fopen(oname, "w");
if (output == NULL)
fatals("Cant open", oname);
#if NEEDBUF
setbuf(output, my_obuf);
#endif
} else
output = stdout;
in_seg = -1;
lblnum = 0;
dat_size = 0;
}
out_end()
{
if (output != stdout)
fclose(output);
}
static char *sg_go[] = {
".text",
".data",
".bss"
};
to_text()
{
TO_TEXT;
}
to_seg(sg)
{
if (sg == in_seg)
return;
fprintf(output, "\t%s\n", sg_go[sg]);
in_seg = sg;
}
o_aln(x)
{
if (x && (dat_size & 1)) {
dat_size++;
TO_DATA;
fprintf(output, "\t.even\n");
}
}
char *rnms[] = {
"d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
"a0", "a1", "a2", "a3", "a4", "a5", "a6", "sp",
};
char *regnm(n)
{
return rnms[n];
}
#define regnm(x) rnms[x]
char *
init_str(n)
{
char *s;
switch (n) {
case 1:
s = ".dc.b"; break;
case 2:
s = ".dc.w"; break;
default:
s = ".dc.l"; break;
}
return s;
}
tlen(n)
{
switch (n) {
case 1:
return 'b';
case 2:
return 'w';
default:
return 'l';
}
}
o_vinit(tp, xp)
NODEP tp, xp;
{
fprintf(output, "\t%s\t", init_str((int)tp->t_size));
dat_size += tp->t_size;
p2_expr(&xp);
asn_chk(tp, xp);
to_init(xp, tp);
fputc('\n', output);
}
to_init(np, typ)
NODEP np, typ;
{
NODEP tp;
tp = allocnode();
tp->e_token = TCONV;
tp->n_tptr = typ;
tp->n_flags |= N_COPYT;
tp->n_left = np;
tp->e_type = E_UNARY;
strcpy(tp->n_name, "i cast");
genx(tp, FORINIT);
}
out_advice(np)
NODEP np;
{
long size;
size = np->n_tptr->t_size;
if (size == 0)
return;
switch (np->e_sc) {
case K_AUTO:
fprintf(output, ";var\t%d\t%d\t", (int)size,
(int)np->e_offs);
break;
case K_REGISTER:
fprintf(output, ";reg\t%d\t%s\t", (int)size,
regnm(np->e_rno));
break;
default:
return;
}
out_nm(np);
fputc('\n', output);
}
out_argreg(np)
NODEP np;
{
fprintf(output, "\tmove.%c\t%d(%s),%s\n",
tlen((int)np->n_tptr->t_size), (int)np->e_offs,
regnm(FRAMEP), regnm(np->e_rno));
}
out_fstart(np)
NODEP np;
{
extern int pflag;
TO_TEXT;
if (np->e_sc != K_STATIC) {
fprintf(output, "\t.globl\t");
und_nnm(np);
fputc('\n', output);
}
und_nnm(np);
fprintf(output, ":\n");
if (pflag) {
int tlab = new_lbl();
TO_BSS;
fprintf(output, "L%d:\t.ds.l\t1\n", tlab);
TO_TEXT;
fprintf(output, "\tmove.l\t#");
und_nnm(np);
fprintf(output, ",a0\n");
fprintf(output, "\tmove.l\t#L%d,a1\n", tlab);
fprintf(output, "\tjsr\tmcount\n");
}
}
static char rbuf[30];
char *
regstr(regs)
{
int lod, hid, loa, hia;
register i;
char *bp = rbuf;
lod = 999;
hid = -1;
for (i=DRV_START; i<=DRV_END; i++)
if (regs & (1<<i)) {
if (i < lod) lod = i;
if (i > hid) hid = i;
}
loa = 999;
hia = -1;
for (i=ARV_START; i<=ARV_END; i++)
if (regs & (1<<i)) {
if (i < loa) loa = i;
if (i > hia) hia = i;
}
if (lod < 999) {
if (lod != hid)
sprintf(bp, "%s-%s", rnms[lod], rnms[hid]);
else
sprintf(bp, "%s", rnms[lod]);
if (loa < 999) {
bp += strlen(rbuf);
*bp++ = '/';
}
}
if (loa < 999) {
if (loa != hia)
sprintf(bp, "%s-%s", rnms[loa], rnms[hia]);
else
sprintf(bp, "%s", rnms[loa]);
}
return rbuf;
}
out_fend(regs, lsize)
long lsize;
{
if (lsize < 0x7fff)
fprintf(output, "\tlink\t%s,#-%d\n", rnms[FRAMEP], (int)lsize);
else
fprintf(output, "\tlink\t%s,#0\n\tsub.l\t#%ld,sp\n",
rnms[FRAMEP], lsize);
if (regs)
fprintf(output, "\tmovem.l\t%s,-(sp)\n", regstr(regs));
}
out_fret(regs, strl)
{
if (regs)
fprintf(output, "\tmovem.l\t(sp)+,%s\n", regstr(regs));
if (strl)
fprintf(output, "\tmove.l\t#L%d,a0\n", strl);
fprintf(output, "\tunlk\t%s\n\trts\n", rnms[FRAMEP]);
}
out_fs(strl, size)
long size;
{
TO_BSS;
def_lbl(strl);
if (size & 1)
fprintf(output, "\t.ds.b\t%ld\n", size);
else
fprintf(output, "\t.ds.w\t%ld\n", size/2);
}
out_gv(np, isbss)
register NODEP np;
{
long sz;
char c;
if (np->e_sc == K_STATIC) {
np->e_offs = lblnum++;
}
if (np->e_sc != K_EXTERN) {
to_seg(isbss ? B_SEG : D_SEG);
if (np->e_sc != K_STATIC) {
fprintf(output, "\t.globl\t");
out_nm(np);
fputc('\n', output);
}
if (isbss) {
if (np->e_sc == K_STATIC) {
out_nm(np);
sz = np->n_tptr->t_size;
c = 'b';
if (np->n_tptr->t_aln) {
c = 'w';
sz /= 2;
}
fprintf(output, ":\t.ds.%c\t%ld\n", c, sz);
} else {
fprintf(output, "\t.comm\t");
out_nm(np);
sz = np->n_tptr->t_size;
if (sz & 1) sz++; /* ALCYON hack */
fprintf(output, ",%ld\n", sz);
}
} else {
out_nm(np);
fprintf(output, ":\n");
}
}
}
new_lbl()
{
return lblnum++;
}
def_lbl(l)
{
fprintf(output, "L%d:\n", l);
}
out_br(l)
{
if (l < 0)
error("bad branch");
else
fprintf(output, "\tbra\tL%d\n", l);
}
static char *bnm[] = {
"",
"beq",
"bne",
"blt",
"bge",
"ble",
"bgt",
"bra",
"nop",
"bcs",
"bcc",
"bls",
"bhi"
};
static char *snm[] = {
"",
"seq",
"sne",
"slt",
"sge",
"sle",
"sgt",
"",
"",
"scs",
"scc",
"sls",
"shi"
};
out_b(key, l)
{
if (key != B_NO)
fprintf(output, "\t%s\tL%d\n", bnm[key], l);
}
out_bnol(key)
{
fprintf(output, "\t%s\t", bnm[key]);
}
out_snol(key)
{
fprintf(output, "\t%s\t", snm[key]);
}
out_d0cmp(x)
{
fprintf(output, "\tcmp.w\t#%d,d0\n", x);
}
out_d0sub(x)
{
fprintf(output, "\tsub.w\t#%d,d0\n", x);
}
out_tlbl(l)
{
fprintf(output, "\t.dc.l\tL%d\n", l);
}
out_tsw()
{
fprintf(output, "\text.l\td0\n");
fprintf(output, "\tasl.l\t#2,d0\n");
fprintf(output, "\tmove.l\t4(pc,d0.l),a0\n");
fprintf(output, "\tjmp\t(a0)\n");
}
out_nm(np)
NODEP np;
{
if (np->e_sc == K_STATIC)
fprintf(output, "L%d", (int)np->e_offs);
else
und_nnm(np);
}
out_zi(tp)
NODEP tp;
{
char *s;
/*
switch (tp->t_token) {
case K_FLOAT:
fprintf(output, "\t.float\t0.0\n"); return;
case K_DOUBLE:
fprintf(output, "\t.double\t0.0\n"); return;
}
*/
dat_size += tp->t_size;
s = init_str((int)tp->t_size);
fprintf(output, "\t%s\t0\n", s);
}
o_nz(sz, aln)
long sz;
{
dat_size += sz;
if (aln) {
if (sz & 1)
fprintf(output, "\t.ds.b\t1\n");
sz >>= 1;
fprintf(output, "\t.ds.w\t%ld\n", sz);
} else {
fprintf(output, "\t.ds.b\t%ld\n", sz);
}
}
dumpstrs(np)
NODEP np;
{
TO_DATA;
more:
if (np == NULL)
return;
fpri